package com.tea.config.wx;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PrivateKey;

import static com.tea.utils.SignUtils.loadPrivateKey;


@Configuration
@ConfigurationProperties(prefix = "wx.pay")
@Data
public class WxPayConfig {
    private String appid;
    private String merchantid;
    private String apiv3key;
    private String privatekey;
    private String merchantserialnumber;
    private String notifyUrl;


    @Bean
    public JsapiServiceExtension jsapiService() throws Exception {
        // 从文件加载私钥（推荐）
        PrivateKey privateKeyFile = loadPrivateKey();
        RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantid)
                .privateKey(privateKeyFile)
                .merchantSerialNumber(merchantserialnumber)
                .apiV3Key(apiv3key)
                .build();
        return new JsapiServiceExtension.Builder().config(config).build();
    }

    @Bean
    public RSAAutoCertificateConfig rsaConfig() throws Exception {
        PrivateKey privateKey = loadPrivateKey(); // 加载私钥
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantid)
                .privateKey(privateKey)
                .merchantSerialNumber(merchantserialnumber)
                .apiV3Key(apiv3key)
                .build();
    }
}